package com.jwell.panrank.presenter


import android.util.Log
import com.jwell.cargoapp.application.UserState
import com.jwell.driverapp.tools.BaseSubscriber
import com.jwell.driverapp.ui.base.BasePresenter
import com.jwell.panrank.application.App
import com.jwell.panrank.contract.LoginContract
import com.jwell.panrank.entity.Account
import com.jwell.panrank.model.LoginModel
import com.jwell.panrank.tools.DataModel
import org.json.JSONObject
import org.reactivestreams.Subscription

/**
 * Created by 76577 on 2018/3/26.
 * 登录
 */
class LoginPresenter : LoginContract.Presenter, BasePresenter<LoginContract.View>() {
    override fun loginByPhoneCode(usernameOrEmailAddress: String, phone: String, phoneCode: String) {
        model.loginByPhoneCode(usernameOrEmailAddress,phone,phoneCode,object :BaseSubscriber(getView()){
            override fun setSubscription(s: Subscription) {
                mSubscription = s
            }

            override fun succeed(data: JSONObject) {
                getView()?.hideProgress()
                var jsonObject: JSONObject? = data.getJSONObject("result")
                if (jsonObject != null) {
                    if (App.DEBUG) println("login-"+jsonObject)
                    //存Token值
                    DataModel.getInstance().saveToken(jsonObject.getString("key"))
                    DataModel.getInstance().saveUserName(jsonObject.getString("name"))
                    DataModel.getInstance().saveIsqtc(jsonObject.getBoolean("isqtc"))
                    DataModel.getInstance().userState = UserState.LOGGED
                    getView()?.loginSuccessful()
                } else {
                    getView()?.loginFailed("未知错误，请新登录")
                }
            }

            override fun onFailed(msg: String) {
                getView()?.loginFailed(msg)
                getView()?.hideProgress()
            }

        });
    }

    override fun sendMsgCode(phone: String, userSessionId: String, msgCode: String) {
        model.sendMsgCode(phone,userSessionId,msgCode,object :BaseSubscriber(getView()){
            override fun succeed(data: JSONObject) {
                getView()?.sendMsgSuccess()
                getView()?.hideProgress()
            }

            override fun onFailed(msg: String) {
                getView()?.loginFailed(msg)
                getView()?.hideProgress()
            }

            override fun setSubscription(s: Subscription) {
                mSubscription = s
            }

        })
    }

    private val model: LoginModel by lazy {
        LoginModel()
    }

    override fun login(userName: String, passWorld: String, isChecked: Boolean) {
        model.login(userName, passWorld, object : BaseSubscriber(getView()) {
            override fun setSubscription(s: Subscription) {
                mSubscription = s
            }

            override fun succeed(data: JSONObject) {
                getView()?.hideProgress()
                var jsonObject: JSONObject? = data.getJSONObject("result")
                if (jsonObject != null) {
                    if (App.DEBUG) println("login-"+jsonObject)
                    //存Token值
                    DataModel.getInstance().saveAccount(Account(userName, passWorld))
                    DataModel.getInstance().saveToken(jsonObject.getString("key"))
                    DataModel.getInstance().saveUserName(jsonObject.getString("name"))
                    DataModel.getInstance().saveIsqtc(jsonObject.getBoolean("isqtc"))
                    DataModel.getInstance().loginCheckBox = isChecked
                    DataModel.getInstance().userState = UserState.LOGGED
                    getView()?.loginSuccessful()
                } else {
                    getView()?.loginFailed("未知错误，请新登录")
                }
            }

            override fun onFailed(msg: String) {
                getView()?.loginFailed(msg)
                getView()?.hideProgress()
            }
        })
    }
}